package com.nari.ies.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.nari.ies.server.constant.Constant;
import com.nari.ies.server.mapper.AreaAndPlantMapper;
import com.nari.ies.server.service.ICurveService;
import com.nari.ies.server.utils.DateUtil;
import com.nari.ies.server.utils.GeneraUtil;
import com.nari.ies.server.utils.XAxisUtil;
import com.nari.newGeneration.tableop.ITableOpNewGen;
import com.nari.newGeneration.tableop.impl.TableOpImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.*;


@Slf4j
@RestController
@RequestMapping("/plant")
public class PlantDataController {

    static ITableOpNewGen tableNet = new TableOpImpl();

    @Autowired
    private ICurveService curveService;

    @Autowired
    private AreaAndPlantMapper areaAndPlantMapper;

    //查询单个风电场站信息  风电场表 223611
    @PostMapping("/getWindPlantInfoById")
    public  Map<String, Object> getWindPlantInfoById(@RequestBody String jsonStr) {

        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String plantId = jsonObject.getString("plantId");
        log.info("传参为："+plantId);
        tableNet.open(1, 1, 1101000, 1, 223611);

        // 总有功、总无功、装机容量、
        // 单机总数、运行台数、检修台、停运台数、
        // 可用容量、运行容量、限电出力、调峰限电出力、网架限电出力、站内限电出力、限电标记位
        Integer[] columnIds = {Constant.WINDPLANT_AND_PVPLANT_NAME.getValue(),
                Constant.WINDPLANT_PGEN.getValue(),Constant.WINDPLANT_QGEN.getValue(),
                Constant.WINDPLANT_AND_PVPLANT_CAPACITY.getValue(),Constant.WINDPLANT_GENERATOR_NUM.getValue(),
                Constant.WINDPLANT_RUN_GN_NUM.getValue(),Constant.WINDPLANT_MAINTENANCE_GN_NUM.getValue(),
                Constant.WINDPLANT_FAULT_GN_NUM.getValue(),Constant.WINDPLANT_AVAIL_POWER.getValue(),
                Constant.WINDPLANT_RUN_CAP.getValue(),Constant.WINDPLANT_LIMIT_POWER.getValue(),
                Constant.WINDPLANT_PEAK_LIMIT.getValue(),Constant.WINDPLANT_GRID_LIMIT.getValue(),
                Constant.WINDPLANT_PLANT_LIMIT.getValue(),Constant.WINDPLANT_IF_LIMIT.getValue()};

        Object[] obj = tableNet.tableGetByKey(plantId, columnIds);
        //log.info("实时库查询数据为："+obj);
        /*===========测试数据开始=========*/
        //Object[] obj  = new Object[]{"陕西1号风电厂站",0.005,0.008,180,50,45,3,2,150,30,8,8,8,8,0};
        /*===========测试数据结束=========*/

        Map<String, Object> maps = new HashMap<>();
        maps.put("name",obj[0]);
        double pnum= Double.parseDouble(obj[1].toString());
        maps.put("p",pnum*1000);
        double qnum=  Double.parseDouble(obj[2].toString());
        maps.put("q",qnum*1000);
        maps.put("capacity",obj[3]);
        maps.put("generator_num",obj[4]);
        maps.put("run_gn_num",obj[5]);
        maps.put("maintenance_gn_num",obj[6]);
        maps.put("fault_gn_num",obj[7]);
        maps.put("avail_power",obj[8]);
        maps.put("run_cap",obj[9]);
        maps.put("limit_power",obj[10]);
        maps.put("peak_limit",obj[11]);
        maps.put("grid_limit",obj[12]);
        maps.put("plant_limit",obj[13]);
        maps.put("if_limit",obj[14]);
        log.info("maps--------------"+maps);
        return maps;
    }

    //查询单个光伏场站信息 光伏电站表 223621
    @PostMapping("/getPvPlantInfoById")
    public  Map<String, Object> getPvPlantInfoById(@RequestBody String jsonStr) {
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String plantId = jsonObject.getString("plantId");
        log.info("传参为："+plantId);
        tableNet.open(1, 1, 1101000, 1, 223621);

        // 总有功、总无功、装机容量、
        // 单机总数、运行台数、检修台、停运台数、
        // 可用容量、运行容量、限电出力、调峰限电出力、网架限电出力、站内限电出力
        Integer[] columnIds ={Constant.WINDPLANT_AND_PVPLANT_NAME.getValue(),
                Constant.PVPLANT_PGEN.getValue(),Constant.PVPLANT_QGEN.getValue(),
                Constant.WINDPLANT_AND_PVPLANT_CAPACITY.getValue(),Constant.PVPLANT_GENERATOR_NUM.getValue(),
                Constant.PVPLANT_RUN_GN_NUM.getValue(),Constant.PVPLANT_MAINTENANCE_GN_NUM.getValue(),
                Constant.PVPLANT_FAULT_GN_NUM.getValue(),Constant.PVPLANT_AVAIL_POWER.getValue(),
                Constant.PVPLANT_RUN_CAP.getValue(),Constant.PVPLANT_LIMIT_POWER.getValue(),
                Constant.PVPLANT_PEAK_LIMIT.getValue(),Constant.PVPLANT_GRID_LIMIT.getValue(),
                Constant.PVPLANT_PLANT_LIMIT.getValue(),Constant.PVPLANT_IF_LIMIT.getValue()};
        Object[] obj = tableNet.tableGetByKey(plantId, columnIds);
        //log.info("实时库查询数据为："+obj);

        /*===========测试数据开始=========*/
        //Object[] obj  = new Object[]{"陕西1号光伏厂站",0.05,0.04,180,50,45,3,2,150,30,8,8,8,8,1};
        /*===========测试数据结束=========*/

        Map<String, Object> maps = new HashMap<>();
        maps.put("name",obj[0]);
        double pnum= Double.parseDouble(obj[1].toString());
        maps.put("p",pnum*1000);
        double qnum= Double.parseDouble(obj[2].toString());
        maps.put("q",qnum*1000);
        maps.put("capacity",obj[3]);
        maps.put("generator_num",obj[4]);
        maps.put("run_gn_num",obj[5]);
        maps.put("maintenance_gn_num",obj[6]);
        maps.put("fault_gn_num",obj[7]);
        maps.put("avail_power",obj[8]);
        maps.put("run_cap",obj[9]);
        maps.put("limit_power",obj[10]);
        maps.put("peak_limit",obj[11]);
        maps.put("grid_limit",obj[12]);
        maps.put("plant_limit",obj[13]);
        maps.put("if_limit",obj[14]);
        log.info("maps--------------"+maps);
        return maps;
    }

    //查询单个风电场站资源信息 测风塔表 223614
    @PostMapping("/getWindPWeatherInfoById")
    public Map<String, Object> getWindPWeatherInfoById(@RequestBody String jsonStr) {
        Map<String, Object> maps = new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String plantId = jsonObject.getString("plantId");
        log.info("开始执行：getWindPWeatherInfoById");
        log.info("传参为："+plantId);
        tableNet.open(1, 1, 1101000, 1, 223614);

        //气温、气压、湿度、轮毂高度风速、轮毂高度风向
        Integer[] columnIds ={Constant.WINDPLANT_TEMPR.getValue(),
                Constant.WINDPLANT_PRESS.getValue(),Constant.WINDPLANT_HUMIDITY.getValue(),
                Constant.WINDPLANT_SPEED_HUB.getValue(),Constant.WINDPLANT_DIRECT_HUB.getValue()};

        List<Map<String, Object>> cfTowerList=areaAndPlantMapper.queryCFTowerId(plantId);


        /*===========测试数据开始=========*/
        //Object[] obj  = new Object[]{30,100,6,80,4};
        /*===========测试数据结束=========*/


         if(cfTowerList!=null && cfTowerList.size()>0){
            log.info("风机ID为："+cfTowerList);

            int size = cfTowerList.size();
            int temprNum=0;
            int pressNum=0;
            int humidityNum=0;
            int speedNum=0;
            int directNum=0;

            double temprTotal=0;
            double pressTotal=0;
            double humidityTotal=0;
            double speedTotal=0;
            double directTotal=0;

            for (Map<String, Object> map : cfTowerList) {
                String cftId = map.get("ID").toString();
                Object[] obj = tableNet.tableGetByKey(cftId, columnIds);
                log.info("实时库查询数据为："+obj);

                if(obj[0]!=null && Double.parseDouble(obj[0].toString())>=-50 && Double.parseDouble(obj[0].toString())<=50){
                    temprTotal+= Double.parseDouble(obj[0].toString());
                }else {
                    temprNum++;
                }

                if(obj[1]!=null && Double.parseDouble(obj[1].toString())>=50 && Double.parseDouble(obj[1].toString())<=150){
                    pressTotal+= Double.parseDouble(obj[1].toString());
                }else {
                    pressNum++;
                }

                if(obj[2]!=null && Double.parseDouble(obj[2].toString())>=0 && Double.parseDouble(obj[2].toString())<=100){
                    humidityTotal+= Double.parseDouble(obj[2].toString());
                }else {
                    humidityNum++;
                }

                if(obj[3]!=null && Double.parseDouble(obj[3].toString())>=0 && Double.parseDouble(obj[3].toString())<=30){
                    speedTotal+= Double.parseDouble(obj[3].toString());
                }else {
                    speedNum++;
                }

                if(obj[4]!=null && Double.parseDouble(obj[4].toString())>=-360 && Double.parseDouble(obj[4].toString())<=360){
                    directTotal+= Double.parseDouble(obj[4].toString());
                }else {
                    directNum++;
                }
            }

            String tempr = "";
            String press = "";
            String humidity = "";
            String speed = "";
            String direct = "";

            if(size - temprNum>0){
                tempr = String.format("%.2f",(temprTotal / (size - temprNum)));
            }

            if(size - pressNum>0){
                press = String.format("%.2f",(pressTotal / (size - pressNum)));
            }
            if(size - humidityNum>0){
                humidity = String.format("%.2f",(humidityTotal / (size - humidityNum)));
            }
            if(size - speedNum>0){
                speed = String.format("%.2f",(speedTotal / (size - speedNum)));
            }
            if(size - directNum>0){
                direct = String.format("%.2f",(directTotal / (size - directNum)));
            }

            maps.put("tempr",tempr);
            maps.put("press",press);
            maps.put("humidity",humidity);
            maps.put("speed_hub",speed);
            maps.put("direct_hub",direct);
        }else {
            maps.put("tempr","");
            maps.put("press","");
            maps.put("humidity","");
            maps.put("speed_hub","");
            maps.put("direct_hub","");
        }
        log.info("maps--------------"+maps);
        return maps;
    }

    //查询单个光伏场站资源信息  光伏气象站表 223624
    @PostMapping("/getPvPlantWeatherInfoById")
    public Map<String, Object> getPvPlantWeatherInfoById(@RequestBody String jsonStr) {
        Map<String, Object> maps = new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String plantId = jsonObject.getString("plantId");
        log.info("传参为："+plantId);
        tableNet.open(1, 1, 1101000, 1, 223624);

        /*===========测试数据开始=========*/
        //Object[] obj  = new Object[]{80, 40};
        /*===========测试数据结束=========*/

        //辐照度、环境温度
        Integer[] columnIds ={Constant.PVPLANT_TEMPR.getValue(), Constant.PVPLANT_IRRA_ALL.getValue()};

        List<Map<String, Object>> gfQXZList=areaAndPlantMapper.querygfQXZId(plantId);
        log.info("光伏气象站ID为："+gfQXZList);

        if(gfQXZList!=null && gfQXZList.size()>0){
            int size = gfQXZList.size();
            int temprNum=0;
            int irraNum=0;

            double temprTotal=0;
            double irraTotal=0;

            for (Map<String, Object> map : gfQXZList) {
                String gfQXZId = map.get("ID").toString();
                Object[] obj = tableNet.tableGetByKey(gfQXZId, columnIds);
                log.info("实时库查询数据为："+obj);

                if(obj[0]!=null && Double.parseDouble(obj[0].toString())>=-50 && Double.parseDouble(obj[0].toString())<=50){
                    temprTotal+= Double.parseDouble(obj[0].toString());
                }else {
                    temprNum++;
                }

                if(obj[1]!=null && Double.parseDouble(obj[1].toString())>=0 && Double.parseDouble(obj[1].toString())<=1500){
                    irraTotal+= Double.parseDouble(obj[1].toString());
                }else {
                    irraNum++;
                }
            }

            String tempr="";
            String irra="";

            if(size - temprNum>0){
                tempr = String.format("%.2f",(temprTotal / (size - temprNum)));
            }

            if(size - irraNum>0){
                irra = String.format("%.2f",(irraTotal / (size - irraNum)));
            }

            maps.put("temp",tempr);
            maps.put("irra_all",irra);

        }else {
            maps.put("temp","");
            maps.put("irra_all","");
        }

        log.info("maps--------------"+maps);

        return maps;
    }

    //查询风机信息 风机表 223613
    @PostMapping("/getWindPlantTurbineInfoById")
    public List<Map<String, Object>> getWindPlantTurbineInfoById(@RequestBody String jsonStr) {
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String plantId = jsonObject.getString("plantId");
        log.info("传参为："+plantId);
        tableNet.open(1, 1, 1101000, 1, 223613);

        // 风电场站id 条件域号
        Integer[] columnConditionIds = {Constant.WIND_TURBINE_WP_ID.getValue()};
        // 待查询域号数组
        // 名称、总有功、总无功、当日电量、容量、风速、风向、运行状态
        Integer[] columnIds = {Constant.WIND_TURBINE_NAME.getValue(),Constant.WIND_TURBINE_P.getValue(),
                Constant.WIND_TURBINE_Q.getValue(),Constant.WIND_TURBINE_DAY_KWH.getValue(),
                Constant.WIND_TURBINE_CAPACITY.getValue(),Constant.WIND_TURBINE_SPEED.getValue(),
                Constant.WIND_TURBINE_DIRECT.getValue(),Constant.WIND_TURBINE_STATE.getValue()};
        // 条件域号对应的域值
        Object[] columnConditionValues = new Object[]{plantId}; //条件域值
        List<Object[]> list = tableNet.conGet(columnConditionIds,columnIds,columnConditionValues);
        log.info("实时库查询数据为："+list);
        /*===========测试数据开始=========*/
        /*ArrayList<Object[]> list = new ArrayList<>();
        list.add(new Object[]{"陕西1号风机",10, 8, 10, 8,10,12,1});
        list.add(new Object[]{"陕西2号风机",10, 8, 10, 8,10,12,1});
        list.add(new Object[]{"陕西3号风机",10, 10, 10, 8,10,12,1});
        list.add(new Object[]{"陕西4号风机",8, 10, 10, 8,10,12,1});
        list.add(new Object[]{"陕西5号风机",11, 7, 10, 8,10,12,1});
        list.add(new Object[]{"陕西6号风机",11, 10, 10, 8,10,12,2});
        list.add(new Object[]{"陕西7号风机",12, 5, 10,6,10,12,2});
        list.add(new Object[]{"陕西8号风机",12, 7, 10,8, 10,12,1});
        list.add(new Object[]{"陕西9号风机",15, 7, 10, 8,10,12,1});
        list.add(new Object[]{"陕西10号风机",11, 7, 10, 8,10,12,2});
        list.add(new Object[]{"陕西11号风机",11, 11, 11, 11,10,12,1});
        list.add(new Object[]{"陕西12号风机",12, 11, 11, 11,10,12,3});
        list.add(new Object[]{"陕西13号风机",14, 11, 11, 11,10,12,3});
        list.add(new Object[]{"陕西14号风机",15, 11, 11, 11,10,12,2});
        list.add(new Object[]{"陕西15号风机",16, 11, 11, 11,10,12,1});
        list.add(new Object[]{"陕西16号风机",12, 11, 11, 11,10,12,4});
        list.add(new Object[]{"陕西17号风机",13, 11, 11, 11,10,12,1});
        list.add(new Object[]{"陕西18号风机",14, 11, 11, 11,10,12,1});
        list.add(new Object[]{"陕西19号风机",15, 11, 11, 11,10,12,1});
        list.add(new Object[]{"陕西20号风机",15, 11, 11, 11,10,12,1});*/
        /*===========测试数据结束=========*/

        ArrayList<Map<String, Object>> lists = new ArrayList<>();
        for (Object[] objects : list) {
            Map<String, Object> maps = new HashMap<>();
            maps.put("name",objects[0]);
            maps.put("p",objects[1]);
            maps.put("q", objects[2]);
            maps.put("day_kwh", objects[3]);
            maps.put("capacity", objects[4]);
            maps.put("speed", objects[5]);
            maps.put("direct", objects[6]);
            maps.put("state", objects[7]);
            lists.add(maps);
        }
        log.info("lists--------------"+lists);
        return lists;
    }

    //查询逆变器信息 光伏逆变器表 223623
    @PostMapping("/getPvPlantInverterInfoById")
    public  List<Map<String, Object>> getPvPlantInverterInfoById(@RequestBody String jsonStr) {
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String plantId = jsonObject.getString("plantId");
        log.info("传参为："+plantId);
        tableNet.open(1, 1, 1101000, 1, 223623);

        // 光伏场站id 条件域号
        Integer[] columnConditionIds = {Constant.PV_INVERTER_PVP_ID.getValue()};
        // 待查询域号数组
        // 总有功、总无功、当日电量、容量
        Integer[] columnIds = {Constant.PV_INVERTER_NAME.getValue(),Constant.PV_INVERTER_P.getValue(),
                Constant.PV_INVERTER_Q.getValue(),Constant.PV_INVERTER_DAY_KWH.getValue(),
                Constant.PV_INVERTER_CAPACITY.getValue(),Constant.PV_INVERTER_STATE.getValue()};
        // 条件域号对应的域值
        Object[] columnConditionValues = new Object[]{plantId}; //条件域值
        List<Object[]> list = tableNet.conGet(columnConditionIds,columnIds,columnConditionValues);
        log.info("实时库查询数据为："+list);
        /*===========测试数据开始=========*/
        /* ArrayList<Object[]> list = new ArrayList<>();
        list.add(new Object[]{"陕西1号逆变器",10, 8, 10, 8,1});
        list.add(new Object[]{"陕西2号逆变器",10, 8, 10, 8,1});
        list.add(new Object[]{"陕西3号逆变器",10, 10, 10, 8,1});
        list.add(new Object[]{"陕西4号逆变器",8, 10, 10, 8,1});
        list.add(new Object[]{"陕西5号逆变器",11, 7, 10, 8,1});
        list.add(new Object[]{"陕西6号逆变器",11, 10, 10, 8,1});
        list.add(new Object[]{"陕西7号逆变器",12, 5, 10, 8,1});
        list.add(new Object[]{"陕西8号逆变器",12, 7, 10, 8,2});
        list.add(new Object[]{"陕西9号逆变器",15, 7, 10, 8,2});
        list.add(new Object[]{"陕西10号逆变器",11, 7, 10, 8,3});
        list.add(new Object[]{"陕西11号逆变器",11, 11, 11, 11,1});
        list.add(new Object[]{"陕西12号逆变器",12, 11, 11, 11,1});
        list.add(new Object[]{"陕西13号逆变器",14, 11, 11, 11,2});
        list.add(new Object[]{"陕西14号逆变器",15, 11, 11, 11,3});
        list.add(new Object[]{"陕西15号逆变器",16, 11, 11, 11,1});
        list.add(new Object[]{"陕西16号逆变器",12, 11, 11, 11,3});
        list.add(new Object[]{"陕西17号逆变器",13, 11, 11, 11,1});
        list.add(new Object[]{"陕西18号逆变器",14, 11, 11, 11,4});
        list.add(new Object[]{"陕西19号逆变器",15, 11, 11, 11,1});
        list.add(new Object[]{"陕西20号逆变器",15, 11, 11, 11,1});*/
        /*===========测试数据结束=========*/

        ArrayList<Map<String, Object>> lists = new ArrayList<>();
        for (Object[] objects : list) {
            Map<String, Object> maps = new HashMap<>();
            maps.put("name",objects[0]);
            maps.put("p",objects[1]);
            maps.put("q", objects[2]);
            maps.put("day_kwh", objects[3]);
            maps.put("capacity", objects[4]);
            maps.put("state", objects[5]);
            lists.add(maps);
        }
        log.info("lists--------------"+lists);
        return lists;
    }

    @PostMapping("/getWindPlantSubUltraData")
    public  Map<String, Object> getWindPlantSubUltraData(@RequestBody String jsonStr) {
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String plantId = jsonObject.getString("plantId");
        Date startTime = jsonObject.getDate("startTime");
        Date startDate = DateUtil.getInitDate(startTime);
        Date endTime = jsonObject.getDate("endTime");
        Date endDate = DateUtil.getNextDay(endTime);
        String yc_id=plantId+"_0041";
        /*===========测试数据开始=========*/
        //String yc_id="22362132000000000257";
        /*===========测试数据结束=========*/
        List<Object> xAxisData = new ArrayList<Object>();
        Map<String, Object> resultObj = new HashMap<String, Object>();

        //橫坐标节点
        int num= (int) ((endDate.getTime() - startDate.getTime()) / (5*60 * 1000));
        int days=(int) ((endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000));
        xAxisData = XAxisUtil.getXAxisData(startDate, endDate, num);

        //-----------------------------------总有功--------------------------------------
        Object[] windDataZYG = curveService.getYcHsCueveData(yc_id, startDate, endDate, 5);
        log.info("总有功数据长度"+windDataZYG.length);
        log.info("总有功数据"+Arrays.toString(windDataZYG));

        //---------------超短期预测---------V0000  开始时间----结束时间
        List<Map<String,Object>> cdqycMapsList =curveService.getDataCDQYC(plantId,startDate,endDate);
        //当前时间节点
        int nodeNum= (int) ((endDate.getTime() - startDate.getTime()) / (15 * 60 * 1000));
        Object[] cdqycDataFirst = new Object[nodeNum];
        if(cdqycMapsList==null||cdqycMapsList.size()==0){
            for (int i = 0 ; i <nodeNum ; i++) {
                cdqycDataFirst[i]=null;
            }
        }else {
            cdqycDataFirst = GeneraUtil.getValueByStep(cdqycMapsList, startDate, cdqycDataFirst);
        }
        log.info("V0000结果集为："+Arrays.toString(cdqycDataFirst));

        //---------超短期预测---  结束时间后四个小时V0000-V0355
        List<LinkedHashMap<String,Object>> DataCDQYCList =curveService.getCDQPoint5Data(plantId,endDate);
        log.info("V0005-V0355结果集为："+DataCDQYCList);
        ArrayList<Object> cdqyclist = new ArrayList<>();
        if(DataCDQYCList!=null&&DataCDQYCList.size()>0){
            LinkedHashMap<String, Object> cdqycMap = DataCDQYCList.get(0);
            log.info("短期预测数据："+cdqycMap);
            Iterator<Map.Entry<String, Object>> iterator = cdqycMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<String, Object> next = iterator.next();
                Object value = next.getValue();
                cdqyclist.add(value);
            }
        }
        //前后拼接
        int length= cdqycDataFirst.length;
        Object[] cdqycDataTotal = new Object[3*length+cdqyclist.size()-3];
        for (int i = 0; i < length-1; i++) {
            if(cdqycDataFirst[i]==null||cdqycDataFirst[i+1]==null){
                cdqycDataTotal[i*3]=cdqycDataFirst[i];
                cdqycDataTotal[i*3+1]=null;
                cdqycDataTotal[i*3+2]=null;
            }else {
                float cdqycNum= (Float.parseFloat(cdqycDataFirst[i+1].toString()) -Float.parseFloat(cdqycDataFirst[i].toString()))/3;
                cdqycDataTotal[i*3]=cdqycDataFirst[i];
                cdqycDataTotal[i*3+1]=Float.parseFloat(cdqycDataFirst[i].toString())+cdqycNum;
                cdqycDataTotal[i*3+2]=Float.parseFloat(cdqycDataFirst[i].toString())+cdqycNum*2;
            }
        }

        for (int i =0; i < cdqyclist.size() ; i++) {
            cdqycDataTotal[length*3-3+i]=cdqyclist.get(i);
        }
        log.info("拼接后长度为"+cdqycDataTotal.length);
        //按照横轴节点取值，多余的不要
        Object[] windDataCDQYC = new Object[num];
        for (int i = 0; i <num ; i++) {
            windDataCDQYC[i]=cdqycDataTotal[i];
        }
        log.info("超短期预测结果集:"+ Arrays.toString(windDataCDQYC));

        //-------------------------------短期预测------------------------------
        Object[] windDataDQYC = new Object[num];
        for (int i = 0; i < days; i++) {
            List<LinkedHashMap<String,Object>> dqycMapList= curveService.getDQPoint5Data(plantId,startDate);
            if(dqycMapList==null|| dqycMapList.size()==0){
                for (int j = i*96 ; j <(i+1)*96 ; j++) {
                    windDataDQYC[j]=null;
                }
            }else {
                LinkedHashMap<String, Object> dqycMap = dqycMapList.get(0);
                log.info("短期预测数据："+dqycMap);
                ArrayList<Object> list = new ArrayList<>();
                Iterator<Map.Entry<String, Object>> iterator = dqycMap.entrySet().iterator();
                while (iterator.hasNext()){
                    Map.Entry<String, Object> next = iterator.next();
                    Object value = next.getValue();
                    list.add(value);
                }
                for (int j = 0; j < 96*3; j++) {
                    windDataDQYC[(i*96*3)+j]=list.get(j);
                }
            }
            startDate = DateUtil.getNextDay(startDate);
        }
        log.info("短期预测结果集:"+ Arrays.toString(windDataDQYC));


        resultObj.put("xAxis", xAxisData);
        resultObj.put("windDataZYG", windDataZYG);
        resultObj.put("windDataCDQYC", windDataCDQYC);
        resultObj.put("windDataDQYC", windDataDQYC);
        return resultObj;
    }

    @PostMapping("/getPvPlantSubUltraData")
    public  Map<String, Object> getPvPlantSubUltraData(@RequestBody String jsonStr) {
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String plantId = jsonObject.getString("plantId");
        Date startTime = jsonObject.getDate("startTime");
        Date startDate = DateUtil.getInitDate(startTime);
        Date endTime = jsonObject.getDate("endTime");
        Date endDate = DateUtil.getNextDay(endTime);

        String yc_id=plantId+"_0041";
        /*===========测试数据开始=========*/
        //String yc_id="22362132000000000257";
        /*===========测试数据结束=========*/
        List<Object> xAxisData = new ArrayList<Object>();
        Map<String, Object> resultObj = new HashMap<String, Object>();

        //橫坐标节点
        int num= (int) ((endDate.getTime() - startDate.getTime()) / (5*60 * 1000));
        int days=(int) ((endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000));
        xAxisData = XAxisUtil.getXAxisData(startDate, endDate, num);

        //-----------------------------------总有功--------------------------------------
        Object[] PvDataZYG = curveService.getYcHsCueveData(yc_id, startDate, endDate, 5);
        log.info("总有功数据长度"+PvDataZYG.length);
        log.info("总有功数据"+Arrays.toString(PvDataZYG));

        //---------------超短期预测---------V0015  开始时间----结束时间
        List<Map<String,Object>> cdqycMapsList =curveService.getDataCDQYC(plantId,startDate,endDate);
        //当前时间节点
        int nodeNum= (int) ((endDate.getTime() - startDate.getTime()) / (15 * 60 * 1000));
        Object[] cdqycDataFirst = new Object[nodeNum];
        if(cdqycMapsList==null||cdqycMapsList.size()==0){
            for (int i = 0 ; i <nodeNum ; i++) {
                cdqycDataFirst[i]=null;
            }
        }else {
            cdqycDataFirst = GeneraUtil.getValueByStep(cdqycMapsList, startDate, cdqycDataFirst);
        }
        log.info("V0000结果集为："+Arrays.toString(cdqycDataFirst));

        //---------超短期预测---  结束时间后四个小时V0000-V0355
        List<LinkedHashMap<String,Object>> DataCDQYCList =curveService.getCDQPoint5Data(plantId,endDate);
        log.info("V0005-V0355结果集为："+DataCDQYCList);
        ArrayList<Object> cdqyclist = new ArrayList<>();
        if(DataCDQYCList!=null&&DataCDQYCList.size()>0){
            LinkedHashMap<String, Object> cdqycMap = DataCDQYCList.get(0);
            log.info("短期预测数据："+cdqycMap);
            Iterator<Map.Entry<String, Object>> iterator = cdqycMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<String, Object> next = iterator.next();
                Object value = next.getValue();
                cdqyclist.add(value);
            }
        }
        //前后拼接
        int length= cdqycDataFirst.length;
        Object[] cdqycDataTotal = new Object[3*length+cdqyclist.size()-3];
        for (int i = 0; i < length-1; i++) {
            if(cdqycDataFirst[i]==null||cdqycDataFirst[i+1]==null){
                cdqycDataTotal[i*3]=cdqycDataFirst[i];
                cdqycDataTotal[i*3+1]=null;
                cdqycDataTotal[i*3+2]=null;
            }else {
                float cdqycNum= (Float.parseFloat(cdqycDataFirst[i+1].toString()) -Float.parseFloat(cdqycDataFirst[i].toString()))/3;
                String format = new DecimalFormat(".00").format(cdqycNum);
                cdqycDataTotal[i*3]=cdqycDataFirst[i];
                cdqycDataTotal[i*3+1]=Float.parseFloat(cdqycDataFirst[i].toString())+Float.parseFloat(format);
                cdqycDataTotal[i*3+2]=Float.parseFloat(cdqycDataFirst[i].toString())+Float.parseFloat(format)*2;
            }
        }

        for (int i =0; i < cdqyclist.size() ; i++) {
            cdqycDataTotal[length*3-3+i]=cdqyclist.get(i);
        }
        log.info("拼接后长度为"+cdqycDataTotal.length);
        //按照横轴节点取值，多余的不要
        Object[] PvDataCDQYC = new Object[num];
        for (int i = 0; i <num ; i++) {
            PvDataCDQYC[i]=cdqycDataTotal[i];
        }
        log.info("超短期预测结果集:"+ Arrays.toString(PvDataCDQYC));

        //-------------------------------短期预测------------------------------
        Object[] pvDataDQYC = new Object[num];
        for (int i = 0; i < days; i++) {
            List<LinkedHashMap<String,Object>> dqycMapList= curveService.getDQPoint5Data(plantId,startDate);
            if(dqycMapList==null|| dqycMapList.size()==0){
                for (int j = i*96 ; j <(i+1)*96 ; j++) {
                    pvDataDQYC[j]=null;
                }
            }else {
                LinkedHashMap<String, Object> dqycMap = dqycMapList.get(0);
                log.info("短期预测数据："+dqycMap);
                ArrayList<Object> list = new ArrayList<>();
                Iterator<Map.Entry<String, Object>> iterator = dqycMap.entrySet().iterator();
                while (iterator.hasNext()){
                    Map.Entry<String, Object> next = iterator.next();
                    Object value = next.getValue();
                    list.add(value);
                }
                for (int j = 0; j < 96*3; j++) {
                    pvDataDQYC[(i*96*3)+j]=list.get(j);
                }
            }
            startDate = DateUtil.getNextDay(startDate);
        }
        log.info("短期预测结果集:"+ Arrays.toString(pvDataDQYC));


        resultObj.put("xAxis", xAxisData);
        resultObj.put("PvDataZYG", PvDataZYG);
        resultObj.put("PvDataCDQYC", PvDataCDQYC);
        resultObj.put("PvDataDQYC", pvDataDQYC);
        return resultObj;
    }

    @PostMapping("/getPlantCurrentData")
    public  Map<String, Object> getPlantCurrentData(@RequestBody String jsonStr) {
        Map<String,Object> result = new HashMap<>();

        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String plantId = jsonObject.getString("plantId");
        System.out.println("plantId:"+plantId);
        Date nowDate = new Date();
        //获取当天0点时间
        Date today = DateUtil.getToday();

        //获取有功实时数据
        tableNet.open(1, 1, 1101000, 1, 223611);
        Integer[] columnIds = {Constant.WINDPLANT_PGEN.getValue()};

        //Object[] obj = tableNet.tableGetByKey(plantId, columnIds);

        /*测试数据*/
        Object[] obj = new Object[]{10};
        /*测试数据*/

        log.info("实时库有功数据："+ Arrays.toString(obj));
        double pgen = Double.parseDouble(obj[0].toString());

        //------------------------短期预测-------------------------
        List<LinkedHashMap<String,Object>> dqycMapList= curveService.getDQPoint5Data(plantId,today);
        //短期预测结果集处理
        Object[] DataDQYC = GeneraUtil.getDQYCData(dqycMapList);
        log.info("短期预测结果集:"+ Arrays.toString(DataDQYC));

        //与初始时间间隔时常-秒级别
        long tempL = (nowDate.getTime() - today.getTime()) / (60 * 1000);
        int nPos = (int) (tempL / 5);
        Object dqData = null;
        if(nPos<=96){
            dqData= DataDQYC[nPos];
        }else {
            dqData=null;
        }

        result.put("pgen",pgen);
        result.put("dqData",dqData);
        System.out.println(result);
        return result;
    }

    @PostMapping("/getPlantPgen")
    public  Map<String, Object> getPlantPgen(@RequestBody String jsonStr) {
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        Date startTime = jsonObject.getDate("startTime");
        Date startDate = DateUtil.getInitDate(startTime);
        Date endTime = jsonObject.getDate("endTime");
        Date endDate = DateUtil.getNextDay(endTime);
        String plantId = jsonObject.getString("plantId");
        String type = jsonObject.getString("type");
        Integer columnId = null;
        Map<String, Object> plantMap = new HashMap<>();
        if(type.equals("wind")){
            tableNet.open(1, 1, 1101000, 1, 223611);
            // 总有功
            columnId = Constant.WINDPLANT_PGEN.getValue();
        }else {
            tableNet.open(1, 1, 1101000, 1, 223621);
            // 总有功
            columnId = Constant.PVPLANT_PGEN.getValue();
        }
        String yc_id=plantId+"_0041";
        Object[] obj = tableNet.tableGetByKey(plantId, columnId);

        /*===========测试数据开始=========*/
        //Object[] obj  = new Object[]{150};
        //String yc_id="22362132000000000257";
        /*===========测试数据结束=========*/

        //获取当前时间
        Date nowDate = new Date();
        log.info("当前时间为"+nowDate);

        List<Object> xAxisData = new ArrayList<Object>();

        //橫坐标节点
        int num= (int) ((endDate.getTime() - startDate.getTime()) / (5*60 * 1000));
        int days=(int) ((endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000));
        xAxisData = XAxisUtil.getXAxisData(startDate, endDate, num);

        Object[] ycHsData = curveService.getYcHsCueveData(yc_id, startDate, endDate, 5);
        log.info("总有功数据长度"+ycHsData.length);
        log.info("总有功数据"+Arrays.toString(ycHsData));
        //-----------------------------------总有功--------------------------------------
        Object[] ZYGData = new Object[96*3*days];

        for (int i = 0; i <ycHsData.length ; i++) {
            ZYGData[i]=ycHsData[i];
        }
        // 计算间隔长度
        int lemp = (int) ((nowDate.getTime() - startDate.getTime()) / (5 * 60 * 1000));
        if(lemp<num){
            ZYGData[lemp]=Double.parseDouble(obj[0].toString());
        }
        log.info("总有功结果集:"+ Arrays.toString(ZYGData));

        plantMap.put("xAxis", xAxisData);
        plantMap.put("ZYGData", ZYGData);

        return plantMap;
    }

    @PostMapping("/getPlantQgen")
    public  Map<String, Object> getPlantQgen(@RequestBody String jsonStr) {
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        Date startTime = jsonObject.getDate("startTime");
        Date startDate = DateUtil.getInitDate(startTime);
        Date endTime = jsonObject.getDate("endTime");
        Date endDate = DateUtil.getNextDay(endTime);
        String plantId = jsonObject.getString("plantId");
        String type = jsonObject.getString("type");
        Integer columnId = null;
        Map<String, Object> plantMap = new HashMap<>();
        if(type.equals("wind")){
            tableNet.open(1, 1, 1101000, 1, 223611);
            // 总无功
            columnId = Constant.WINDPLANT_QGEN.getValue();
        }else {
            tableNet.open(1, 1, 1101000, 1, 223621);
            // 总无功
            columnId = Constant.PVPLANT_QGEN.getValue();
        }
        String yc_id=plantId+"_0043";
        Object[] obj = tableNet.tableGetByKey(plantId, columnId);

        /*===========测试数据开始=========*/
        //Object[] obj  = new Object[]{120};
        //String yc_id="22362132000000000257";
        /*===========测试数据结束=========*/

        //获取当前时间
        Date nowDate = new Date();
        log.info("当前时间为"+nowDate);

        List<Object> xAxisData = new ArrayList<Object>();

        //橫坐标节点
        int num= (int) ((endDate.getTime() - startDate.getTime()) / (5*60 * 1000));
        int days=(int) ((endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000));
        xAxisData = XAxisUtil.getXAxisData(startDate, endDate, num);

        Object[] ycHsData = curveService.getYcHsCueveData(yc_id, startDate, endDate, 5);
        log.info("总有功数据长度"+ycHsData.length);
        log.info("总有功数据"+Arrays.toString(ycHsData));
        //-----------------------------------总有功--------------------------------------
        Object[] ZWGData = new Object[96*3*days];

        for (int i = 0; i <ycHsData.length ; i++) {
            ZWGData[i]=ycHsData[i];
        }
        // 计算间隔长度
        int lemp = (int) ((nowDate.getTime() - startDate.getTime()) / (5 * 60 * 1000));
        if(lemp<num){
            ZWGData[lemp]=Double.parseDouble(obj[0].toString());
        }
        log.info("总有功结果集:"+ Arrays.toString(ZWGData));

        plantMap.put("xAxis", xAxisData);
        plantMap.put("ZWGData", ZWGData);

        return plantMap;
    }
}
